package com.wlld;


import com.alibaba.fastjson.JSONObject;
import com.wlld.config.Config;
import com.wlld.entity.BoxBody;
import com.wlld.entity.ThreeChannelMatrix;
import com.wlld.entity.YoloBody;
import com.wlld.tools.Picture;
import com.wlld.yolo.YoloByWLLD;

import java.io.*;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) throws Exception {//一层的长度变化 （图片长度-（核长-步长））/步长
        //test();
        a();
    }

    public static void test() throws Exception {
        Config config = new Config();
        Picture picture = new Picture();
        YoloByWLLD yoloByWLLD = new YoloByWLLD(config);
        yoloByWLLD.init();
        //第一次训练
        System.out.println("进行第一步训练");
        ThreeChannelMatrix backGround = picture.getThreeMatrix("/Users/lidapeng/Desktop/file/background.jpg");
        yoloByWLLD.setBackGround(backGround);
        for (int i = 1; i < 322; i++) {
            System.out.println("第一轮===" + i);
            String url = "/Users/lidapeng/Desktop/file/1/" + i + ".jpg";
            YoloBody yoloBody = readPaper(url + ".json");
            yoloBody.setBoxType(2);
            yoloByWLLD.insertFeature(yoloBody, url);
            yoloByWLLD.studyConv();
            yoloByWLLD.clear();
        }
        System.out.println("进行第二步训练");
        for (int i = 1; i < 322; i++) {
            System.out.println("第二轮===" + i);
            String url = "/Users/lidapeng/Desktop/file/1/" + i + ".jpg";
            YoloBody yoloBody = readPaper(url + ".json");
            yoloBody.setBoxType(2);
            yoloByWLLD.insertFeature(yoloBody, url);
            yoloByWLLD.studyDnn();
            yoloByWLLD.clear();
        }
        //学习过程结束
        System.out.println("开始测试==================");
        for (int i = 1; i < 8; i++) {
            System.out.println("单个==================" + i);
            String url = "/Users/lidapeng/Desktop/test/a" + i + ".jpg";
            ThreeChannelMatrix threeChannelMatrix1 = picture.getThreeMatrix(url);
            yoloByWLLD.look(threeChannelMatrix1, url, "a" + i);
        }
        for (int i = 1; i < 8; i++) {
            System.out.println("两个==================" + i);
            String url = "/Users/lidapeng/Desktop/test/b" + i + ".jpg";
            ThreeChannelMatrix threeChannelMatrix1 = picture.getThreeMatrix(url);
            yoloByWLLD.look(threeChannelMatrix1, url, "b" + i);
        }
        for (int i = 1; i < 8; i++) {
            System.out.println("三个==================" + i);
            String url = "/Users/lidapeng/Desktop/test/c" + i + ".jpg";
            ThreeChannelMatrix threeChannelMatrix1 = picture.getThreeMatrix(url);
            yoloByWLLD.look(threeChannelMatrix1, url, "c" + i);
        }
    }

    public static YoloBody readPaper(String fileName) throws Exception {
        File file = new File(fileName); //创建文件
        Reader read = null;
        StringBuilder stringBuilder = new StringBuilder();
        try {
            //一次读一个字符
            read = new InputStreamReader(new FileInputStream(file));
            int tempchar;
            while ((tempchar = read.read()) != -1) {
                stringBuilder.append((char) tempchar);
            }
            read.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (read != null) {
                try {
                    read.close(); //确保关闭
                } catch (IOException el) {
                }
            }
        }
        YoloBody yoloBody = JSONObject.parseObject(
                stringBuilder.toString(), YoloBody.class);
        return yoloBody;
    }

    public static void a() {//锚点坐标，中心距, 7 43
        double x = 125;//61 29
        double kernLen = 5;
        double step = 2;
        int times = 0;
        do {
            x = (x - (kernLen - step)) / step;
            if (x > 0) {
                System.out.println("x:" + x);
                times++;
            }
        } while (x > 0);
        System.out.println("times:" + times);
    }
}
